package IndusmecaSI.Indusmeca_Model
{
	import IndusmecaSI.Indusmeca_Type.User;
	
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.messaging.channels.StreamingAMFChannel;
	import IndusmecaSI.Indusmeca_Type.Company;
	import IndusmecaSI.Indusmeca_Type.User;
	
	public class Model_User_Service
	{
		private var myServiceUser:NetConnection;	
		private var _result:Function;
		private var _fault:Function;
		
		public function Model_User_Service(result:Function, fault:Function)
		{
			_result = result;
			_fault = fault;
			myServiceUser = new NetConnection();
			myServiceUser.objectEncoding = ObjectEncoding.AMF3;
			myServiceUser.connect("http://flex.jutech.lan/amfphp1.9/gateway.php");
		}
		public function getAllUser():void
		{
			var responder:Responder = new Responder(getAllUser_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.getAllUser", responder);
		}
		public function getUserByID(id_item:int):void
		{
			var responder:Responder = new Responder(getUserByID_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.getUserByID", responder,id_item);
		}
		public function createUser(item:User):void
		{
			var responder:Responder = new Responder(createUser_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.createUser", responder,item);
		}
		public function getUserByUser(user:User):void
		{
			var responder:Responder = new Responder(getUserByUser_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.getUserByUser", responder,user.login,user.password,user.id_company);
		}
		public function getAllCompany():void
		{
			var responder:Responder = new Responder(getAllCompany_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.getAllCompany",responder);
	
		}
		public function getCompanyCombo():void
		{
			var responder:Responder = new Responder(getCompanyCombo_Result, onFault);
			myServiceUser.call("INDUSMECA.UserService.getAllCompany",responder);
			
		}
		private function getAllCompany_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormCompany(value));
			}
			
			_result(6,res);	
		}
		private function getCompanyCombo_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem({label: value.raison_sociale, data: value.id_company});
			}
			
			_result(5,res);	
		}
		private function getAllUser_Result(result:Object):void
		{
			var res:ArrayCollection = new ArrayCollection();
			for each( var value:* in result)
			{
				res.addItem(miseEnFormUser(value));
			}
			_result(3,res);
		}
		private function getUserByID_Result(result:Object):void
		{
			_result(2,miseEnFormUser(result));
		}
		private function createUser_Result(result:Object):void
		{
			_result(1,result);
		}
		private function getUserByUser_Result(result:Object):void
		{
			
			_result(4,miseEnFormUser(result[0]));
		}
		private function miseEnFormUser(value:Object):User
		{
			var user:User = new User();
			user.date_inscription = value.date_inscription;
			user.email =value.email;
			user.emplacement =value.emplacement;
			user.firstname = value.firstname;
			user.id_company = value.id_company;
			user.id_user = value.ID_USER;
			user.login = value.LOGIN;
			user.name =value.name;
			user.password = value.PASSWORD;
			user.raison_sociale = value.raison_sociale;
			trace(value.LOGIN+' '+value.PASSWORD+' '+value.id_company);
			return user;
		}
		private function miseEnFormCompany(value:Object):Company
		{
			var cie:Company = new Company();
			cie.id_company = value.id_company;
			cie.emplacement =value.emplacement;
			cie.raison_sociale =value.raison_sociale;
			
			return cie;
		}
		
		
		public function onFault(obj : *):void{
			
		//	trace('ERREUR :'+obj.faultDetail + obj.faultString);
			_fault(obj.faultDetail +"\n\n"+obj.faultString);
		}
	}
}
